package com.devplatform.mapper.points;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.devplatform.pojo.points.PointsDetail;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 86156
 * @description 针对表【points_detail】的数据库操作Mapper
 * @createDate 2025-09-28 14:44:21
 * @Entity com.devplatform.pojo.PointsDetail
 */
public interface PointsDetailMapper extends BaseMapper<PointsDetail> {

    @Select("SELECT COUNT(*) FROM points_detail " +
            "WHERE user_id = #{userId} " +
            "AND reason_code LIKE 'SIGN_%' " +
            "AND create_time BETWEEN #{start} AND #{end}")
    int countSignInToday(
            @Param("userId") Long userId,
            @Param("start") LocalDateTime start,
            @Param("end") LocalDateTime end
    );

    @Select("  SELECT * FROM points_detail\n" +
            "    WHERE user_id = #{userId}\n" +
            "    ORDER BY create_time DESC")
    Page<PointsDetail> selectByUserId(Page<PointsDetail> page, @Param("userId") Long userId);

    @Select("SELECT * FROM points_detail " +
            "WHERE user_id = #{userId} " +
            "AND reason_code = #{reasonCode} " +
            "ORDER BY create_time DESC")
    Page<PointsDetail> selectByUserIdAndReason(
            Page<PointsDetail> page,
            @Param("userId") Long userId,
            @Param("reasonCode") String reasonCode
    );
    @Select("SELECT * FROM points_detail WHERE detail_id = #{detailId} AND user_id = #{userId}")
    PointsDetail selectByDetailId(@Param("detailId") Integer detailId, @Param("userId") Long userId);

    List<PointsDetail> selectSignInRecordsByUserId(@Param("userId") Long userId);

    @Select("SELECT COUNT(*) FROM points_detail " +
            "WHERE user_id = #{userId} " +
            "AND reason_code LIKE 'SIGN_%' " +
            "AND create_time >= DATE_FORMAT(CURDATE(), '%Y-%m-01')")
    int countSignInByMonth(@Param("userId") Long userId);


    // 统计总签到次数（只统计签到相关记录）

    @Select("SELECT COUNT(*) FROM points_detail " +
            "WHERE user_id = #{userId} " +
            "AND reason_code LIKE 'SIGN_%'")
    int countTotalSignIn(@Param("userId") Long userId);

    @Select("SELECT COUNT(*) FROM points_detail " +
            "WHERE user_id = #{userId} AND reason_code = #{reasonCode} " +
            "AND create_time BETWEEN #{start} AND #{end}")
    int countByReasonToday(@Param("userId") Long userId,
                           @Param("reasonCode") String reasonCode,
                           @Param("start") LocalDateTime start,
                           @Param("end") LocalDateTime end);

    // 统计本月获得的正积分总和
    @Select("SELECT COALESCE(SUM(points_change), 0) FROM points_detail " +
            "WHERE user_id = #{userId} " +
            "AND points_change > 0 " +
            "AND DATE_FORMAT(create_time, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m')")
    Integer sumMonthEarned(@Param("userId") Long userId);

    // 统计本月消耗的负积分绝对值总和
    @Select("SELECT COALESCE(ABS(SUM(points_change)), 0) FROM points_detail " +
            "WHERE user_id = #{userId} " +
            "AND points_change < 0 " +
            "AND DATE_FORMAT(create_time, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m')")
    Integer sumMonthSpent(@Param("userId") Long userId);
}




